//
// Created by THE13 on 2024/2/6.
//

#include "page.h"


/*global param*/
uint16_t    page_cnt = 0;
_sys_info_t sys_info;

/*private function*/
static menu_t* add_item(menu_t *head,const char * name);

/*1.添加头节点*/
menu_t* AddNode(menu_t *node,const char * name)
{
    menu_t * p = add_item(node,name);
    return p;
}

/*2.添加页面*/
menu_t* AddPage(menu_t *node,PageType type,const char *name)
{
    int i=0;//page id
    menu_t *pr;//temp ptr

    auto ani_p = (SecondOrderDynamics_t *)malloc(sizeof(SecondOrderDynamics_t)) ;
    /*1. allocate momery*/
    auto p = (menu_t *)malloc(sizeof(menu_t)) ;
    if(p == nullptr)return nullptr;


    /*2.allocate ptr*/
    if(node->next == nullptr){
        node->next=p;
    }
    else{
        pr = node->next;i++;
        while(pr->same_next!= nullptr)
        {
            i++;
            pr=pr->same_next;
        }
        pr->same_next = p;
        p ->same_last = pr;
    }

    /*3.init param*/
    p->last=node;
    p->next=nullptr;
    p->same_next=nullptr;
    p->id = i;//页面ID 0~N
    p->state = onViewLoard;//页面状态
    p->name=name;//页面名字
    p->pagetype=type;//页面类型
    p->callback=nullptr;
    p->pos_x = 10;
    p->pos_y = 20+(i)*(10+10);
    p->ani = ani_p;

    return p;//返回页面
}

/*3.添加选项*/
void AddItem(struct _menu *page,ItemType itemType,const char *name,uint16_t* iconSrc,uint16_t param1,uint16_t param2,void (* callback)())
{
    int i=0;//item id
    menu_t *p,*pr = page;

    auto ani_p = (SecondOrderDynamics_t *)malloc(sizeof(SecondOrderDynamics_t)) ;

    /*1.allocate momery*/
    p = (menu_t *)malloc(sizeof(menu_t)) ;
    if(p == nullptr){
    }
    /*2.allocate ptr*/
    if(pr->next == nullptr){
        pr->next=p;
    }
    else{
        pr = pr->next;i++;
        while(pr->same_next!= nullptr)
        {
            i++;
            pr=pr->same_next;
        }
        pr->same_next = p;
        p ->same_last = pr;
    }
    /*3.init param*/
    p->same_next=nullptr;
    p->last=page;
    p->next=nullptr;
    p->id = i;//ID 0~N
    p->state = onViewLoard;//状态
    p->name=name;//名字
    p->param1 = param1;//参数1
    p->param2 = param2;//参数2
    p->pos_x = 20;
    p->pos_y = 60+(i)*(10+10);
    p->itemtype=itemType;//
    p->callback=callback;
    p->ani = ani_p;
    p->iconSrc = iconSrc;
    if(page->pagetype== ICON)
    {
        p->pos_x = 20+p->id*90;
    }
}


/* 添加一个功能 head为该界面的第一项，自动在该界面后添加一项功能 --------------------------------*/
static menu_t* add_item(menu_t *head,const char * name)
{
    menu_t *pr = head;
    auto  p = (menu_t *)malloc(sizeof(menu_t)) ;
    if(p == nullptr){
        return nullptr;
    }
    if(head == nullptr){
        head = p;
    }else{
        int i=1;
        while(pr->same_next!= nullptr){
            {
                i++;
                pr = pr->same_next;
            }

        }
        pr->same_next = p;
        p->same_last=pr;
        p->last=head->last;
        p->id=i;
    }
    p->name=name;
    p->same_next = nullptr;
    p->next=nullptr;
    return head;
}

void SysInfo_init(menu_t *node)
{
    sys_info.now = node->next->next;//初始化索引指针为第一页的第一项
    sys_info.ctrl = 0;
}